package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.qztech.sdk.R;
import com.qztech.utils.YzDebug;

public class XoverFilterChangeView extends RadioGroup implements CompoundButton.OnCheckedChangeListener {
    final int XOVER_FILTER_TYPE_BESSEL    = 0;
    final int XOVER_FILTER_TYPE_BUTTER_W  = 1;
    final int XOVER_FILTER_TYPE_LINK_RIL  = 2;
    private RadioGroup rdogSlope;
    private int[] valueIds = {com.hanlink.res.R.id.rbtn_1, com.hanlink.res.R.id.rbtn_2, com.hanlink.res.R.id.rbtn_3, com.hanlink.res.R.id.rbtn_4, com.hanlink.res.R.id.rbtn_5, com.hanlink.res.R.id.rbtn_6};
    private RadioButton[] rbtns = new RadioButton[valueIds.length];
//    private int mValue = 6;
    private int mCheckIndex = 0;
    public XoverFilterChangeView(Context context) {
        super(context);
        initView();
    }

    public XoverFilterChangeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    protected void initView() {
        LayoutInflater.from(getContext()).inflate(com.hanlink.res.R.layout.layout_xover_linkwitz, this, true);
        rdogSlope = findViewById(com.hanlink.res.R.id.rdogSlope);

        for (int i = 0; i < valueIds.length; i++) {
            rbtns[i] = findViewById(valueIds[i]);
            rbtns[i].setOnCheckedChangeListener(this);
        }
    }
    public void updateByFilterType(int filterType){
        switch (filterType){
            case XOVER_FILTER_TYPE_LINK_RIL:
                rbtns[2].setVisibility(View.GONE);
                rbtns[4].setVisibility(View.GONE);
                break;
            case XOVER_FILTER_TYPE_BESSEL:
            case XOVER_FILTER_TYPE_BUTTER_W:
                rbtns[2].setVisibility(View.VISIBLE );
                rbtns[4].setVisibility(View.VISIBLE );
                break;
        }
    }
    public void updateSlopeSelected(int index){
        mCheckIndex = index;
        if (index >=0 && index < valueIds.length){
            rbtns[index].setChecked(true);
        }else {
            rdogSlope.check(-1);
        }
    }


    private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener;
    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
//        rdogSlope.setOnCheckedChangeListener(onCheckedChangeListener);
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!isChecked)return;
        mCheckIndex = Integer.parseInt(buttonView.getTag().toString());

        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
        }
    }



    public int getCheckIndex() {
        return mCheckIndex;
    }

//    public int getValue() {
//        return mValue;
//    }

    @Override
    public void setEnabled(boolean enabled) {
        rdogSlope.setEnabled(enabled);
        for (RadioButton rbtn : rbtns) {
            rbtn.setEnabled(enabled);
        }
    }



    public void setFilterVisibility(int index, int visibility) {
        if (rbtns.length > index) {
            rbtns[index].setVisibility(visibility);
        }
    }



}
